import websocket


import threading
import time

import global_config


# 定义连接关闭时的回调函数
def on_close(ws, close_status_code, close_msg):
    print("### closed ###")


# 定义连接建立时的回调函数
def on_open(ws):
    print("Connection opened...")


def start_comfyui_client(on_message, on_error):
    # WebSocket服务器的URL
    ws_url = f"{global_config.websocket_url}?clientId=22e94456aa354acf9c6f766be9fab157"
    print(ws_url)
    # 创建一个websocket连接
    ws = websocket.WebSocketApp(ws_url,
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close)

    # 创建线程用于运行websocket的监听循环
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

